﻿using B.S.RbacData.ErrorCode;
using B.S.RbacData.Write.API.Applications.Commands.RBAC;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.RbacData.Write.API.Controllers
{

    /// <summary>
    /// 权限管理（写）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RBACMenganmentController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<RBACMenganmentController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public RBACMenganmentController(ILogger<RBACMenganmentController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }


        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPermission(AddPermissionCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加权限异常处理" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 编辑权限
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdatePermission(UpdatePermissionCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("编辑权限异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除权限
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> IsDelPermission(IsDelCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除权限异常处理" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]

        public async Task<ApiResult<int>> AddRolepermission(AddRolePermissionCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加角色信息异常处理" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 编辑角色信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateRolePermission(UpdateRolePermissionCommand command)
        {
            try
            {
                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("编辑角色信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除角色信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>

        [HttpPost]
        public async Task<ApiResult<int>> IsDelRole(IsDelRoleCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除角色信息异常处理" + ex.Message);
                throw;
            }


        }


        /// <summary>
        ///新增用户信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUser(AddUserCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("新增用户信息异常处理" + ex.Message);
                throw;
            }


        }


        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>

        [HttpPost]
        public async Task<ApiResult<int>> UpdateUser(UpdateUserCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("编辑用户信息异常处理" + ex.Message);
                throw;
            }


        }


        /// <summary>
        /// 逻辑删除用户信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> IsDelUser(IsDelUserCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除用户信息异常处理" + ex.Message);
                throw;
            }
        }



    }
}
